﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace TextEditor
{
    public partial class TextEditorForm : Form
    {
        string filename = "";
        String originalContent;
        string content;
        int backColor = 222;

        public TextEditorForm()
        {
            InitializeComponent();
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.DefaultExt = "*.*";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                setFilename( ofd.FileName );
                BinaryReader br = new BinaryReader(File.Open(filename, FileMode.Open));
                String s = new String(br.ReadChars((int)br.BaseStream.Length));
                originalContent = s;
                
                br.Close();
                setContent(s);
            
            }

        }

        private void setFilename(string f)
        {
            filename = f;
            this.Text = "TextEditor - " + filename;
        }

        public string getContent()
        {
            return textBox1.Text.Replace("\u00d8", "\0");
        }

        public void setContent(string c)
        {
            content = c;
            generateRtf();
        }

        public void generateRtf()
        {
            string rtf = content;
            rtf = rtf.Replace("\0", "\\highlight2 \u00d8\\highlight1 ");
            rtf = rtf.Replace("\r\n", "\n");
            rtf = rtf.Replace("\n", "\\par\r\n");

            rtf = "{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang2057{\\fonttbl{\\f0\\fnil\\fcharset0 Courier New;}}\r\n{\\colortbl ;\\red" + backColor.ToString() + "\\green" + backColor.ToString() + "\\blue" + backColor.ToString() + ";\\red215\\green30\\blue30;}\r\n\\viewkind4\\uc1\\pard\\highlight1\\f0\\fs17 " + rtf +
                "\\highlight0\\par\r\n}\r\n";

            textBox1.Rtf = rtf;
        }

        private void findToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FindDialog fd = new FindDialog();
            fd.textEditorForm = this;
            fd.Show();
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (filename == "")
            {
                SaveFileDialog sfd = new SaveFileDialog();
                if (sfd.ShowDialog() != DialogResult.OK)
                    return;
                setFilename( sfd.FileName );
            }
            BinaryWriter bw = new BinaryWriter(File.Open(filename, FileMode.Create));
            bw.Write(getContent().ToCharArray());
            bw.Close();
        }

        private void revertToSavedToolStripMenuItem_Click(object sender, EventArgs e)
        {
            setContent(originalContent);
        }



        private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
        {
            textBox1.SelectionBackColor = Color.FromArgb(backColor, backColor, backColor);
        }

        private void wrapToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (wrapToolStripMenuItem.Checked == true)
                wrapToolStripMenuItem.Checked = false;
            else
                wrapToolStripMenuItem.Checked = true;
            textBox1.WordWrap = wrapToolStripMenuItem.Checked;
        }
    }
}
